package com.ymttest.database.mongo.shenqi;

import java.util.HashMap;
import java.util.Map;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.WriteResult;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

/**
 * SyncCommand
 * 
 *订单同步指令记录表
 */
public class SyncCommandWapper {

	MongoDBDriver mdd;

	public SyncCommandWapper() throws Exception {
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip"),
				Integer.valueOf(EnvSetup.getSystemUrl("set5_mongoport")),
				"Order");
		mdd.selectCollection("SyncCommand");
		mdd.auth("Orderuser", "123456");
	}

	public SyncCommandWapper(String cmd) throws Exception {
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip"),
				Integer.valueOf(EnvSetup.getSystemUrl("set5_mongoport")),
				"Order");

		mdd.selectCollection("SyncCmd");
		mdd.auth("Orderuser", "123456");
	}

	/**
	 * 根据条件查询一条订单同步指令结果记录
	 * 
	 * @param id
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public Map getSyncCommand(String cmdId) throws Exception {

		Map re = new HashMap();

		BasicDBObject queryObject = new BasicDBObject();

		queryObject.put("_id", new ObjectId(cmdId));

		DBCursor querycursor = mdd.getCollection().find(queryObject);

		while (querycursor.hasNext()) {

			re = querycursor.next().toMap();
			return re;

		}

		return null;
	}

	/**
	 * 根据条件查询一条新订单同步指令结果记录
	 * 
	 * @param id
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public Map getTradingSyncCommand(long orderId) throws Exception {

		Map re = new HashMap();

		BasicDBObject queryObject = new BasicDBObject();

		queryObject.put("oid", orderId);

		BasicDBObject queryObject2 = new BasicDBObject();

		queryObject2.put("at", -1);

		DBCursor querycursor = mdd.getCollection().find(queryObject)
				.sort(queryObject2);

		while (querycursor.hasNext()) {

			re = querycursor.next().toMap();

			return re;
		}
		return null;
	}
	
	/**
	 * 根据条件查询一条新订单同步指令结果记录
	 * 
	 * @param id
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getTradingSyncCommand(Long orderId, String command) throws Exception {

		Map<String, Object> re = new HashMap<>();

		BasicDBObject queryObject = new BasicDBObject();

		queryObject.put("oid", orderId);
		queryObject.put("cmd", command);
		queryObject.put("ost", 1);

		BasicDBObject queryObject2 = new BasicDBObject();

		queryObject2.put("at", -1);

		DBCursor querycursor = mdd.getCollection().find(queryObject)
				.sort(queryObject2);

		while (querycursor.hasNext()) {

			re = querycursor.next().toMap();

			return re;
		}
		
		return null;
	}
	
	public Integer updatePlimitByInaid(long orderId) throws Exception {
		
		WriteResult wr = mdd.update("{\"est\":" + -1 + "}", "{\"est\":" + 3 + "}",false, true);
		return wr.getN();
	}
	
	protected void close() throws Throwable {
		mdd.close();
	}

	public static void main(String args[]) {
		try {

			Map<String, Object> map = new SyncCommandWapper("cmd")
					.getTradingSyncCommand(11212748699L, "LOGISTICS_CODE");

			Logger.info("map:" + map);
			
//			int num = new SyncCommandWapper("cmd").updatePlimitByInaid(2477918699L);
//			Logger.info("num:" + num);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
